/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package quests.Q023_LidiasHeart;

import net.sf.l2j.gameserver.model.actor.L2Npc;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.model.quest.Quest;
import net.sf.l2j.gameserver.model.quest.QuestState;
import net.sf.l2j.gameserver.model.quest.State;
import net.sf.l2j.gameserver.network.serverpackets.NpcSay;

public class Q023_LidiasHeart extends Quest
{
	private final static String qn = "Q023_LidiasHeart";
	
	//NPCs
	private static final int Innocentin = 31328;
	private static final int BrokenBookshelf = 31526;
	private static final int GhostofvonHellmann = 31524;
	private static final int Tombstone = 31523;
	private static final int Violet = 31386;
	private static final int Box = 31530;
	
	//Items
	private static final int  MapForestofDeadman = 7063;
	private static final int SilverKey = 7149;
	private static final int LidiaHairPin = 7148;
	private static final int LidiaDiary = 7064;
	private static final int SilverSpear = 7150;
	private static final int Adena = 57;
	
	public Q023_LidiasHeart(int questId, String name, String descr)
	{
		super(questId,name,descr);
		
		questItemIds = new int[] { MapForestofDeadman, SilverKey, LidiaHairPin, LidiaDiary, SilverSpear };
		
		addStartNpc(Innocentin);
		addTalkId(Innocentin, BrokenBookshelf, GhostofvonHellmann, Violet, Box, Tombstone);
	}
	
	@Override
	public String onAdvEvent(String event, L2Npc npc, L2PcInstance player)
	{
		String htmltext = event;
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		if (event.equalsIgnoreCase("31328-02.htm"))
		{
			st.giveItems(MapForestofDeadman, 1);
            st.giveItems(SilverKey, 1);
            st.set("cond", "1");
            st.playSound("ItemSound.quest_accept");
            st.setState(State.STARTED);
		}
		else if (event.equalsIgnoreCase("31328-03.htm"))
		{
			st.set("cond", "2");
            st.playSound("ItemSound.quest_middle");
		}
		else if (event.equalsIgnoreCase("31526-05.htm"))
		{
			if (st.getQuestItemsCount(LidiaHairPin) == 0)
				st.giveItems(LidiaHairPin, 1);
			if (st.getQuestItemsCount(LidiaDiary) != 0)
			{
				st.set("cond", "4");
                st.playSound("ItemSound.quest_middle");
			}
		}
		else if (event.equalsIgnoreCase("31526-11.htm"))
		{
			if (st.getQuestItemsCount(LidiaDiary) == 0)
				st.giveItems(LidiaDiary, 1);
			if (st.getQuestItemsCount(LidiaHairPin) != 0)
			{
				st.set("cond", "4");
                st.playSound("ItemSound.quest_middle");
			}
		}
		else if (event.equalsIgnoreCase("31328-19.htm"))
		{
			st.set("cond", "6");
            st.playSound("ItemSound.quest_middle");
		}
		else if (event.equalsIgnoreCase("31524-04.htm"))
		{
			st.set("cond", "7");
            st.playSound("ItemSound.quest_middle");
            st.takeItems(LidiaDiary, -1);
		}
		else if (event.equalsIgnoreCase("31523-02.htm"))
		{
			st.playSound("SkillSound5.horror_02");
            L2Npc ghost = st.addSpawn(31524, 51432, -54570, -3136, 1800000);
            ghost.broadcastPacket(new NpcSay(ghost.getObjectId(),0,ghost.getNpcId(),"Who awoke me?"));
		}
		else if (event.equalsIgnoreCase("31523-05.htm"))
			st.startQuestTimer("ghost_timer", 10000);
		else if (event.equalsIgnoreCase("ghost_timer"))
		{
			st.set("cond", "8");
            htmltext = "31523-06.htm";
            st.giveItems(SilverKey, 1);
		}
		else if (event.equalsIgnoreCase("31530-02.htm"))
		{
			st.set("cond", "10");
            st.playSound("ItemSound.quest_middle");
            st.takeItems(SilverKey, -1);
            st.giveItems(SilverSpear, 1);
		}
		
		return htmltext;
	}
	
	@Override
	public String onTalk(L2Npc npc, L2PcInstance player)
	{
		String htmltext = Quest.getNoQuestMsg(player);
		QuestState st = player.getQuestState(qn);
		if (st == null)
			return htmltext;
		
		if (st.isCompleted())
		{
			htmltext = Quest.getAlreadyCompletedMsg(player);
			return htmltext;
		}
		
		int cond = st.getInt("cond");
		
		switch (st.getState())
		{
		case State.CREATED:
			QuestState st2 = player.getQuestState("Q022_TragedyInVonHellmannForest");
			if (st2.isCompleted())
			{
				if (st.getPlayer().getLevel() >= 64)
					htmltext = "31328-01.htm";
				else
				{
					htmltext = "31328-00.htm";
					st.exitQuest(true);
				}
			}
			else
			{
				htmltext = "31328-00.htm";
				st.exitQuest(true);
			}
			break;
			
		case State.STARTED:
			switch (npc.getNpcId())
			{
			case Innocentin:
				if (cond == 1)
					htmltext = "31328-03.htm";
				else if (cond == 2)
					htmltext = "31328-07.htm";
				else if (cond == 4)
					htmltext = "31328-08.htm";
				else if (cond == 6)
					htmltext = "31328-19.htm";
				break;
				
			case BrokenBookshelf:
				if (cond == 2)
				{
					htmltext = "31526-00.htm";
					st.playSound("ItemSound.quest_middle");
					st.set("cond", "3");
				}
				else if (cond == 3)
				{
					if (st.getQuestItemsCount(LidiaHairPin) == 0 && st.getQuestItemsCount(LidiaDiary) == 0)
						htmltext = "31526-02.htm";
					else if (st.getQuestItemsCount(LidiaHairPin) != 0 && st.getQuestItemsCount(LidiaDiary) == 0)
						htmltext = "31526-06.htm";
					else if (st.getQuestItemsCount(LidiaHairPin) == 0 && st.getQuestItemsCount(LidiaDiary) != 0)
						htmltext = "31526-12.htm";
				}
				else if (cond == 4)
					htmltext = "31526-13.htm";
				break;
				
			case GhostofvonHellmann:
				if (cond == 6)
					htmltext = "31524-01.htm";
				else if (cond == 7)
					htmltext = "31524-05.htm";
				break;
				
			case Tombstone:
				if (cond == 6)
				{
					if (st.getQuestTimer("spawn_timer") != null)
						htmltext = "31523-03.htm";
					else
						htmltext = "31523-01.htm";
				}
				else if (cond == 7)
					htmltext = "31523-04.htm";
				else if (cond == 8)
					htmltext = "31523-06.htm";
				break;
				
			case Violet:
				if (cond == 8)
				{
					htmltext = "31386-01.htm";
					st.playSound("ItemSound.quest_middle");
					st.set("cond", "9");
				}
				else if (cond == 9)
					htmltext = "31386-02.htm";
				else if (cond == 10)
				{
					if (st.getQuestItemsCount(SilverSpear) != 0)
					{
						st.setState(State.COMPLETED);
						htmltext = "31386-03.htm";
						st.takeItems(SilverSpear, -1);
						st.rewardItems(Adena, 350000);
						st.addExpAndSp(456893, 42112);
						st.exitQuest(false);
						st.playSound("ItemSound.quest_finish");
					}
					else
						htmltext = "You have no Silver Spear...";
				}
				break;
				
			case Box:
				if (cond == 9)
					htmltext = "31530-01.htm";
				else if (cond == 10)
					htmltext = "31386-03.htm";
				break;
			}
			break;
		}
		
		return htmltext;
	}
	
	public static void main(String[] args)
	{
		new Q023_LidiasHeart(23, qn, "Lidias Heart");
	}
}